﻿using Client.Constant;
using Client.Framework.Event;
using Client.Framework.Message;
using Client.Framework.Socket;
using Client.Message.Request;
using Client.Message.Response;
using Client.Model;
using Client.Service;
using Client.View;
using Client.View.CommonView;
using Client.View.MenuView;
using Client.View.PermissionView;
using Client.View.UserView;
using Client.ViewModel.UserViewModel;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace Client.ViewModel
{
    /// <summary>
    /// Author: Leil
    /// Description: 主页面数据绑定类
    /// CreateTime: 2020/5/13 00:46:11
    /// </summary>
    public class MainWindowViewModel : BindableBase
    {

        private List<MainMenuItem> mainMenuItems;

        public List<MainMenuItem> MainMenuItems
        {
            get { return mainMenuItems; }
            set
            {
                mainMenuItems = value;
                RaisePropertyChanged("MainMenuItems");
            }
        }


        /// <summary>
        /// 主页面
        /// </summary>
        private UserControl mainPage;
        public UserControl MainPage
        {
            get { return mainPage; }
            set
            {
                mainPage = value;
                RaisePropertyChanged("MainPage");
            }
        }


        private ViewUser currentLoginUser;

        public ViewUser CurrentLoginUser
        {
            get { return currentLoginUser; }
            set
            {
                currentLoginUser = value;
                RaisePropertyChanged("CurrentLoginUser");
            }
        }

        public MainWindow CurrentWindow { get; set; }
        public MainWindowViewModel(MainWindow mainWindow)
        {
            CurrentWindow = mainWindow;
            RequestLogoutCommand = new DelegateCommand(RequestLogout);
            MsgEventSubscribe();
            GetMenusTree();
        }

        /// <summary>
        /// 消息事件订阅
        /// </summary>
        private void MsgEventSubscribe()
        {
            MessageEventPublisher.SubscribeEvent(MessageId.RESPONSE_MENUS_TREE, MenusTreeResponse);
            MessageEventPublisher.SubscribeEvent(MessageId.RESPONSE_LOGOUT, ResponseLogout);
        }

        private void InitMenus(List<MainMenu> mainMenus)
        {
            System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                MainMenuItems = mainMenus.Select(mm =>
                {
                    return new MainMenuItem(mm, this);
                }).ToList();
                // 初始化首页
                MainPage = ScreenService.Instance().GetScreenPage("HOME_PAGE");
            }));
        }


        private void GetMenusTree()
        {
            MenusTreeReq msg = new MenusTreeReq(MessageId.REQUEST_MENUS_TREE)
            {
                IsAll = 2
            };
            ApplicationClient.Instance().Send(msg);
        }


        private void MenusTreeResponse(ResponseBaseMessage message)
        {
            if (message is MenusTreeResp msg)
            {
                if (msg.IsAll == 2)
                {
                    InitMenus(msg.MainMenus);
                }
            }
        }



        public DelegateCommand RequestLogoutCommand { get; set; }
        private void RequestLogout()
        {
            LogoutReq msg = new LogoutReq(MessageId.REQUEST_LOGOUT);
            ApplicationClient.Instance().Send(msg);
        }


        private void ResponseLogout(ResponseBaseMessage message)
        {
            if (message is LogoutResp msg)
            {
                if (msg.Code == 200)
                {
                    // 将客户端内存中的用户清除
                    ApplicationClient.Instance().CurrentLoginUser = null;
                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        CurrentWindow.LoginWindow.Show();
                        CurrentWindow.Close();
                    }));
                } else if (msg.Code == 500)
                {
                    Console.WriteLine(msg.ErrMsg);
                }
            }
        }


    }
}
